<?php
class Add extends Controller {
	public function html() {
		$mUser = new User_model();
		$currUser = $mUser->currUser();
		if(!$currUser) header('Location:/auth/login?backurl='.urlencode('/yunying/page/add'));
		
		$data = array();
		$tpl = 'yunying/page/info.html';
		
		//获取所有分类
		$mArticle = new Article_model();
		$category = $mArticle->category();
		if($category) $_category = $category;
		else  $_category = array();
		$data['category'] = array();
		foreach($_category as $key => $value) {
			$data['category'][$value['cid']] = $value['name'];
		}
		
		$this->display($data, $tpl);
	}
	public function json() {
		$title = $this->post('title', 'str');
		$cid = $this->post('cid', 'int', 0);
		$filename = $this->post('filename', 'str');
		$keywords = $this->post('keywords', 'str');
		$description = $this->post('description', 'str');
		$content = htmlspecialchars_decode($this->post('content', 'str'));
		
		if(!$title) $this->displayJSON(array('status' => -1, 'message' => '请输入页面标题'));
		if(!$filename) $this->displayJSON(array('status' => -2, 'message' => '请选择文件名称'));
		if(!$content) $this->displayJSON(array('status' => -3, 'message' => '请输入页面内容'));
		
		$mArticle = new Article_model();
		
		//检查页面名称是否存在
		$params = array('filename' => $filename);
		$result = $mArticle->ArticleInfo($params);
		if($result) $this->displayJSON(array('status' => -4, 'message' => '页面文件名称已存在'));
		
		$params = array(
			'title' 		=> $title,
			'cid' 			=> $cid,
			'filename' 		=> $filename,
			'keywords'		=> $keywords,
			'description' 	=> $description,
			'content' 		=> $content,
			'created_time'  => Func::time()
		);
		$result = $mArticle->create($params);
		if($result) $this->displayJSON(array('status' => 0, 'message' => '添加页面成功'));
		else $this->displayJSON(array('status' => -5, 'message' => '添加页面失败'));
	}
}